let storage = bui.storage();
let language, org, height, uiList,translate;
loader.define(function (require, exports, module, global) {
    let pageview = {
        init() {
            height = $(window).height() -75;
            org = storage.get("org",0)
            translate = global.translate
            language = storage.get("language", 0)
            if (!language) {
                language = 'chinese_simplified'
            }

            try {
                translate.selectLanguageTag.show = false;
                translate.ignore.id.push('languageContent')
                let curLanguage = translate.language.getCurrent()
                if (curLanguage !== language) {
                    translate.changeLanguage(language)
                }
                translate.execute();
            }catch (e) {
                console.log("切换语言报错")
            }
            let languageArr = storage.get('languageArr',0)
            let html = ''
            let html2 = '<select class="languageSelect" id="languageSelectId">\n'
            if (language !== 'chinese_simplified' && language !== 'english') {
                languageArr.forEach(lan => {
                    if (lan.id === language) {
                        html += '<span class="language" id="'+lan.id+'">'+lan.name+'</span>|'
                    } else {
                        html2 += '<option value="'+lan.id+'">'+lan.name+'</option>'
                    }
                })
            } else {
                languageArr.forEach(lan => {
                    html2 += '<option value="'+lan.id+'">'+lan.name+'</option>'
                })
            }
            html+= '<span class="language" id="english">English</span>|<span class="language" id="chinese_simplified">简体中文</span>'
            router.$("#languageContent").html(html + html2 + "</select>")

            router.$(".language").click(function () {
                let selectId = $(this).attr('id')
                if (selectId !== language) {
                    storage.set("language", selectId)
                    location.reload()
                }
            })
            router.$("#languageSelectId").val("");
            router.$("#languageSelectId").change(function () {
                let selectId = $(this).val()
                if (selectId !== language) {
                    storage.set("language", selectId)
                    location.reload()
                }
            })


            router.$('#sear').click(function(event){//监听sim卡回车事件
                let str = router.$("#search").val()
                uiList.replace({
                    data:{city: org.id,
                        country: org.parent,
                        title: str,
                        redisCity: org.name,
                        'isfix1': '1',
                        isduplicate: 0,
                        cate: 1403
                    }
                });
            });
            router.$('#search').bind('keypress',function(event){//监听sim卡回车事件
                if(event.keyCode == "13")
                {
                    let str = $(this).val()
                    uiList.replace({
                        data:{city: org.id,
                            country: org.parent,
                            title: str,
                            redisCity: org.name,
                            'isfix1': '1',
                            isduplicate: 0,
                            cate: 1403
                        }
                    });
                }
            });

            scList('', global.api.getArticleList)
        }
    }
    // 初始化
    pageview.init();

    // 输出模块
    return pageview;
})

function formatDate(time) {
    let date = new Date(time * 1000);
    let Y = date.getFullYear() + '-';
    let M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
    let D = (date.getDate() < 10 ? '0'+(date.getDate()) : date.getDate()) + ' ';
    let h = (date.getHours() < 10 ? '0'+(date.getHours()) : date.getHours()) + ':';
    let m = (date.getMinutes() < 10 ? '0'+(date.getMinutes()) : date.getMinutes()) + ':';
    let s = (date.getSeconds() < 10 ? '0'+(date.getSeconds()) : date.getSeconds());
    return Y+M+D+h+m+s
}

function scList(search, url) {
    uiList = bui.list({
        id: "#scrollEntList",
        url: url,
        height: height,
        pageSize: 10, // 当pageSize 小于返回的数据大小的时候,则认为是最后一页,接口返回的数据最好能返回空数组,而不是null
        data: {
            city: org.id,
            redisCity: org.name,
            country: org.parent,
            'isfix1': '1',
            isduplicate: 0,
            cate: 1403,
            title: search
        },
        //如果分页的字段名不一样,通过field重新定义
        field: {
            page: "page",
            size: "limit",
            data: "data"
        },
        callback: function(e) {
            // e.target 为你当前点击的元素
            // e.currentTarget 为你当前点击的handle 整行
            let id = router.$(e.currentTarget).attr("id")
            let name = router.$(e.currentTarget).attr("name")
            router.load({ url: "pages/articleInfo/articleInfo.html", param: {id: id, glUserid: name, tzSign: '0'}});

        },
        template: function(data) {
            let picClass = 'imgBJPC'
            if (isMobile()) {
                picClass = 'imgBJ'
            }

            let fontSize = '0.3rem'
            let fontSize2 = '0.24rem'
            let fontSize3 = '0.2rem'
            if (language !== "chinese_simplified") {
                fontSize = '0.22rem'
                fontSize2 = '0.18rem'
                fontSize3 = '0.12rem'
            }

            let html = "";
            data.forEach(function(el, index) {
                html += '<li class="bui-btn bui-box-vertical" id="'+el.id+'" name="'+el.glUserid+'">\n' +
                    '                            <div style="min-height: 60px">\n'
                if (el.zd == '0') {
                    html += '                                <div class="zdK" style="font-size: '+fontSize+'">荐</div>\n'
                }
                search = router.$("#search").val()
                if (search) {
                    let titles = el.title.split(search)
                    html += '<p style="color: black;font-size: '+fontSize+';margin-bottom: 0.22rem">'
                    titles.forEach(function(el, index) {
                        html += '<span>'+el+'</span>'
                        if (index < titles.length - 1) {
                            html += '<span style="color: red;">'+search+'</span>'
                        }
                    })

                    if (el.contentText) {
                        html += '<p style="color: rgb(120,120,120);font-size: '+fontSize3+';">'

                        let contentTexts = el.contentText.split(search)
                        let xzLength = 100
                        if (el.contentText.length < xzLength) {
                            contentTexts.forEach(function(str, index) {
                                html += '<span>'+str+'</span>'
                                if (index < contentTexts.length - 1) {
                                    html += '<span style="color: red;">'+search+'</span>'
                                }
                            })
                        } else {
                            let len = search.length
                            contentTexts.forEach(function(el, index) {
                                if (len <= xzLength) {
                                    if (el.length > xzLength - len) {
                                        let str = el.substring(el.length - (xzLength - len), el.length)
                                        html += '<span>...'+str+'</span>'
                                    } else {
                                        html += '<span>'+el+'</span>'
                                    }
                                    len += el.length
                                    if (index < contentTexts.length - 1) {
                                        html += '<span style="color: red;">'+search+'</span>'
                                    }
                                }
                            })
                        }
                        html += '<p>'
                    }
                } else {
                    html += '                        <p style="color: black;font-size: '+fontSize+';margin-bottom: 0.22rem">'+el.title+'</p>\n'
                    if (el.contentText) {
                        html += '                        <p style="color: rgb(120,120,120);font-size: '+fontSize3+';margin-bottom: 0.22rem">'+el.contentText.substring(0,100)
                        if(el.contentText.length > 100) {
                            html += '...'
                        }
                        html += '</p>\n'
                    }
                }

                html += '                   <p class="item-text">\n' +
                    '                                    <span class="listTab" style="font-size: '+fontSize2+'">'+el.cateStr+'</span>\n' +
                    '                                    <span style="font-size: '+fontSize2+'">\n' + formatDate(el.ptime) + '</span>\n' +
                    '                                    <span style="font-size: '+fontSize2+'">'+org.description+'</span>\n' +
                    '                                </p>\n' +
                    '                            </div>\n'
                if (el.imgs && el.imgs.length > 0) {
                    html += '                          <div class="span1">\n'+
                        '                                <div class="bui-box">\n'
                    let len = el.imgs.length > 3 ? 3 : el.imgs.length
                    for (let i = 0; i < len; i++) {
                        html +=  '                                    <div class="span1 '+picClass+'">\n' +
                            '                                        <img src="'+el.imgs[i]+'?x-oss-process=style/index" alt="">\n' +
                            '                                    </div>\n'
                    }

                    if (len < 3) {
                        for (let i = 0; i < 3- len; i++) {
                            html +=  '                                    <div class="span1 '+picClass+'">\n' +
                                '                                    </div>\n'
                        }
                    }

                    html += '                                </div>\n' +
                        '                            </div>\n' +
                        '                        </li>'
                } else {
                    html +=
                        '                        </li>'
                }
            });
            setTimeout(function () {
                try {
                    translate.execute();
                }catch (e) {
                    console.log("切换语言报错")
                }
            },1000)
            return html;
        }
    })
}

function isMobile() {
    let userAgentInfo = navigator.userAgent;

    let mobileAgents = ["Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod"];

    let mobile_flag = false;

    let ua = navigator.userAgent.toLowerCase()
    if (ua.match(/MicroMessenger/i) == "micromessenger") {
        mobile_flag = true
    } else {
        //根据userAgent判断是否是手机
        for (let v = 0; v < mobileAgents.length; v++) {
            if (userAgentInfo.indexOf(mobileAgents[v]) > 0) {
                mobile_flag = true;
                break;
            }
        }
    }

    return mobile_flag;
}
